﻿using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OpenAdmin.Application.Services;
using OpenAdmin.Application.Services.Dtos;

namespace OpenAdmin.HttpApi.Controllers;

/// <summary>
/// 账号相关Api
/// </summary>
[AllowAnonymous, ApiExplorerSettings(GroupName = "Account")]
public class AccountController : ApiControllerBase
{
    private readonly IAccountService _userService;
    /// <summary>
    /// 账号相关
    /// </summary>
    /// <param name="userService"></param>
    public AccountController(IAccountService userService)
    {
        _userService = userService;
    }

    /// <summary>
    /// 登录
    /// </summary>
    /// <param name="requestDto"></param>
    /// <returns></returns>
    [HttpPost, ProducesResponseType(typeof(LoginResponseDto), 200)]
    public async Task<IActionResult> LoginAsync(LoginRequestDto requestDto)
    {
        return Ok(await _userService.LoginAsync(requestDto));
    }

    /// <summary>
    /// 刷新token
    /// </summary>
    /// <param name="token"></param>
    /// <returns></returns>
    [HttpPost, ProducesResponseType(typeof(LoginResponseDto), 200)]
    public async Task<IActionResult> RefreshTokenAsync([Required(ErrorMessage = "token不能为空")] string token)
    {
        return Ok(await _userService.RefreshTokenAsync(token));
    }
}
